package com.dp;

import org.junit.jupiter.api.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedissionTest2 {
    @Resource
    RedissonClient redissonClient;
    @Test
    public void test01(){
//        创建可重入锁
        RLock testLock = redissonClient.getLock("test");
//        尝试获取锁
        boolean isLock = false;
        try {
            //参数：获取锁最大等待时间，锁释放时间，单位
            isLock = testLock.tryLock(1,20, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(isLock){
            try{
                System.out.println("执行业务");
            }finally {
                testLock.unlock();
            }
        }
    }
}
